******
* author: Anthony Harding (tony.harding@gatech.edu)
* Georgia Institute of Technology, School of Public Policy


* This script generates regression coefficients and tables
* Requires estout, estwide, ftools, ivreg2, reghdfe, and ivreghdfe packages

* 1st Imports Data
* 2nd Runs baseline regressions
* Outputs data for Table 1
******


clear all
set mem 1G
set matsize 10000
set maxvar 10000

* Set working directory
cd "\\Client\C$\Users\aharding6\Dropbox (GaTech)\Tobetransferred\ConsistentClimateConvergence - Copy\Replication"




*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
* 1. Import and prep data
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 1 - Import main data
*-------------------------------------------------------------------------------
* Import main dataset (source Burke et al. 2015) - contains temperature and growth variables
use data/input/GrowthClimateDataset, clear

*-------------------------------------------------------------------------------
* End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 2 - Generate new useful variables
*-------------------------------------------------------------------------------
* Generate country indicator and xtset data
xtset iso_id year

* Make precip in micrometers
replace UDel_precip_popweight = UDel_precip_popweight/1000
replace UDel_precip_popweight_2 = UDel_precip_popweight_2/1000^2


* Generate new economic variables
qui gen poor = (GDPpctile_WDIppp<50)
qui replace poor=. if GDPpctile_WDIppp==.
qui gen log_gdpCAP_wdi = log(gdpCAP_wdi)
qui gen loggdpCAP_wdi_l = l.log_gdpCAP_wdi

* Generate new weather variables
qui gen UDel_temp_popweight_2 = UDel_temp_popweight^2
gen poor_temp = poor*UDel_temp_popweight
gen poor_prec = poor*UDel_precip_popweight
gen poor_temp2 = poor*UDel_temp_popweight_2
gen poor_prec2 = poor*UDel_precip_popweight_2
gen dtemp = UDel_temp_popweight-l.UDel_temp_popweight
gen dprec = UDel_precip_popweight-l.UDel_precip_popweight
gen dtemp2 = UDel_temp_popweight_2-l.UDel_temp_popweight_2
gen dprec2 = UDel_precip_popweight_2-l.UDel_precip_popweight_2
gen poor_dtemp = poor*dtemp
gen poor_dprec = poor*dprec
gen poor_dtemp2 = poor*dtemp2
gen poor_dprec2 = poor*dprec2

* Generate new year variables
gen timegr = time/(1+time)
gen timegrdiff = timegr-l.timegr
gen timegr2 = 1/2*time^2/(1+time)
*gen time2 = time^2
gen year2 = year^2
gen yeargr = year/(1+year)


gen timeex = time*exp(-1/50*(time))
gen yearex = year*exp(-1/50*(year-1960))
gen yearexdiff = yearex-l.yearex

gen timeex2 = exp(-1/50*(time))
gen timegrb = time/50/(1+time/50)

*-------------------------------------------------------------------------------
* End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 3 - Rename and relabel key variables
*-------------------------------------------------------------------------------
* Rename variables
ren UDel_temp_popweight temp
ren UDel_temp_popweight_2 temp2
ren UDel_precip_popweight prec
ren UDel_precip_popweight_2 prec2

* Relabel variables
label variable temp "Temp"
label variable temp2 "Temp Sq."
label variable prec "Precip"
label variable prec2 "Precip Sq."
label variable dtemp "$\Delta$Temp"
label variable dtemp2 "$\Delta$Temp Sq."
label variable dprec "$\Delta$Precip"
label variable dprec2 "$\Delta$Precip Sq."
label variable poor "poor"
label variable poor_temp "Temp*Poor"
label variable poor_prec "Precip*Poor"
label variable poor_temp2 "Temp Sq.*Poor"
label variable poor_prec2 "Precip Sq.*Poor"
*-------------------------------------------------------------------------------
* End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 4 - Export prepped dataset for projections
*-------------------------------------------------------------------------------
* Save as .dta file

save "data/output/PreppedDataset", replace

preserve
keep year iso iso_id temp prec growthWDI gdpCAP_wdi loggdpCAP_wdi_l TotGDP
export delimited using "data/output/mainDataset.csv", replace
restore

*-------------------------------------------------------------------------------
* End Step 4 - End Step 4 - End Step 4 - End Step 4 - End Step 4 - End Step 4 
*-------------------------------------------------------------------------------



*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
* 2. Run and Store Regressions
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 1 - Pooled SR, no country time trend, no GDP lag
*-------------------------------------------------------------------------------
*** Column 1 ***
eststo: reghdfe growthWDI temp temp2 prec prec2, absorb(i.year  i.iso_id) vce(cluster iso_id)
estadd local TimeTrend "None", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[temp]/(-2*_b[temp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 2 - Pooled SR, linear country time trend, no GDP lag
*-------------------------------------------------------------------------------
*** Column 2 ***
eststo: reghdfe growthWDI temp temp2 prec prec2, absorb(i.year  i.iso_id i.iso_id#c.time) vce(cluster iso_id)
estadd local TimeTrend "Linear", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[temp]/(-2*_b[temp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 3 - Pooled SR, quadratic country time trend, no GDP lag
*-------------------------------------------------------------------------------
*** Column 3 ***
eststo: reghdfe growthWDI temp temp2 prec prec2, absorb(i.year  i.iso_id i.iso_id#c.time i.iso_id#c.time2) vce(cluster iso_id)
estadd local TimeTrend "Quadratic", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[temp]/(-2*_b[temp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 4 - Pooled SR, no country time trend, 1 GDP lag
*-------------------------------------------------------------------------------
*** Column 4 ***
eststo: ivreghdfe growthWDI temp temp2 prec prec2 (l.log_gdpCAP_wdi = l3.log_gdpCAP_wdi), absorb(i.year  i.iso_id)  cluster(iso_id)
estadd local TimeTrend "None", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[temp]/(-2*_b[temp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 4 - End Step 4 - End Step 4 - End Step 4 - End Step 4 - End Step 4 
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 5 - Pooled SR, linear country time trend, 1 GDP lag
*-------------------------------------------------------------------------------
*** Column 5 ***
eststo: ivreghdfe growthWDI temp temp2 prec prec2 (l.log_gdpCAP_wdi = l3.log_gdpCAP_wdi), absorb(i.year  i.iso_id i.iso_id#c.time) cluster(iso_id)
estadd local TimeTrend "Linear", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[temp]/(-2*_b[temp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 5 - End Step 5 - End Step 5 - End Step 5 - End Step 5 - End Step 5
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 6 - Pooled LR, convergent country time trend, 1 GDP lag
*-------------------------------------------------------------------------------
*** Column 6 ***
eststo: ivreghdfe growthWDI dtemp dtemp2 dprec dprec2 temp temp2 prec prec2 (l.growthWDI = l3.growthWDI), absorb(i.year  i.iso_id) cluster(iso_id)
estadd local TimeTrend "None", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[dtemp]/(-2*_b[dtemp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 6 - End Step 6 - End Step 6 - End Step 6 - End Step 6 - End Step 6
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 7 - Rich/poor LR, no country time trend, 1 GDP lag
*-------------------------------------------------------------------------------
*** Column 7 ***
eststo: ivreghdfe growthWDI i.poor#c.(dtemp dtemp2 dprec dprec2 temp temp2 prec prec2) (i.poor#c.l.growthWDI = i.poor#c.l3.growthWDI), absorb(i.year  i.iso_id) cluster(iso_id)
estadd local TimeTrend "None", replace
estadd local RichPoor "Rich/Poor", replace
estadd local MaxTemp=round(_b[1.poor#c.dtemp]/(-2*_b[1.poor#c.dtemp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 7 - End Step 7 - End Step 7 - End Step 7 - End Step 7 - End Step 7
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 8 - Export Regression Coefficients
*-------------------------------------------------------------------------------
* Export coefficients for tables and figures
esttab using "data/output/RegressionCoeff_Text.csv", drop(_cons) not noobs plain replace

* Export regression results to LaTex Table
estwide using "tablesandfigures/RegressionCoeff_Text.tex", se ar2 drop(_cons) s(MaxTemp TimeTrend N r2 r2_a, label("Max GDP Growth Temp" "Country-Specific Time Trend" "Obs."  "R sq." "Adj. R sq.")) label addnote("All models include country and year fixed effects. Standard Errors are clustered at the country level. Temperature is measured in C. Precipitation is measured in $\mu$m/year") replace

* Clear results
eststo clear
*-------------------------------------------------------------------------------
* End Step 8 - End Step 8 - End Step 8 - End Step 8 - End Step 8 - End Step 8
*-------------------------------------------------------------------------------








